#include "Task.h"
#include "Utils.hpp"

namespace nio {

	Task::Task(int64_t interval, bool repeated, std::function<void()> func) {
		this->interval = interval;
		this->nextTime = Utils::timestampMS() + interval;
		this->repeated = repeated;
		this->func = func;
	}

	int64_t Task::RemainTime() {
		return nextTime - Utils::timestampMS();
	}

	void Task::Cancel() {
		cancelled = true;
	}

	void Task::Execute() {
		if (cancelled)return;
		func();
	}

	bool Task::tryFinish() {
		if (cancelled)return true;
		if (repeated) {
			nextTime = Utils::timestampMS() + interval;
			return false;
		}
		return true;
	}

}